<?php
/**
 * 管理员
 * Created by PhpStorm.
 * User: ZengYou
 * Date: 2018/10/13 0013
 * Time: 12:45
 */
namespace app\common\validate;

use app\common\facade\model\AdminModel;

class Admin extends BaseValidate
{

    /**
     * 姓名唯一
     */
    protected function nameUnique($value, $rule, $data)
    {

        $where[] = ['name', '=', $value];

        $info = AdminModel::info($where, ['id']);

        if (!empty($info)) exception('姓名已存在');

        return true;
    }

    protected $field = [
        'name'      => '姓名',
        'password'  => '密码',
        'password2' => '确认密码',
        'enable'    => '启用',
        'roleId'    => '角色',
        'id'        => 'ID',
    ];

    protected $rule = [
        'name'       => 'require|max:20|nameUnique',
        'password'   => 'require|alphaDash|min:8',
        'password2'  => 'requireWith:password|confirm:password',
        'enable'     => 'require|in:2,3',
        'roleId'     => 'require|number|max:10',
        'id'     => 'require|number|max:10',
    ];

    protected $message = [
        'password2.confirm' => "确认密码和密码不一致"
    ];

    /**
     * 登录
     */
    protected function sceneLogin()
    {
        return $this->only([
            'name',
            'password'
        ])->remove([
            'name'     => 'nameUnique',
            'password' => 'min',
        ]);
    }

    /**
     * 添加【保存】
     */
    protected function sceneAddSave()
    {
        return $this->only([
            'name',
            'password',
            'password2',
            'enable',
            'roleId'
        ]);
    }

    /**
     * 编辑【保存】
     */
    protected function sceneEditSave()
    {
        return $this->only([
            'id',
            'password',
            'password2',
            'enable',
            'roleId'
        ])->remove([
            'password' => 'require'
        ]);
    }

    /**
     * 修改密码【保存】
     */
    protected function scenePasswordEditSave()
    {
        return $this->only([
            'password',
            'password2',
        ]);
    }




}
